package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.domain.condition.subaccount.SubAccountCondition;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.filecenter.domain.form.subaccount.SubAccountCreateForm;
import com.bwda.dsrs.filecenter.domain.form.subaccount.SubAccountQueryForm;
import com.bwda.dsrs.filecenter.domain.form.subaccount.SubAccountUpdateForm;
import com.bwda.dsrs.filecenter.domain.po.subaccount.SubAccountPo;
import com.bwda.dsrs.filecenter.domain.vo.subaccount.SubAccountVo;
import com.bwda.dsrs.filecenter.service.SubAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
/**
 *
 *
 * @author system
 * @date 2019/4/26
 */
@RestController
@RequestMapping("/b/subaccount")
@Api(value = "/b/subaccount", tags = "null")
public class SubAccountController extends BaseController {

    @Autowired
    private SubAccountService subAccountService;

    @ApiOperation(value = "查询null", notes = "查询null", httpMethod = "GET")
    @RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<SubAccountVo> query(@ApiParam(value = "ID", required = true) @RequestParam Integer id) throws BusinessServiceException {
        SubAccountPo po = subAccountService.queryWithValid(id);
        SubAccountVo vo = CopyUtil.transfer(po, SubAccountVo.class);
        return getSuccessResult(vo);
    }


    @ApiOperation(value = "查询null数量", notes = "查询null数量", httpMethod = "GET")
    @RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<Integer> queryCount(@ModelAttribute @Valid SubAccountQueryForm form) throws BusinessServiceException {
        SubAccountCondition condition = this.getConditionByQueryForm(form);
        int count = subAccountService.queryCount(condition);
        return getSuccessResult(count);
    }

    @ApiOperation(value = "查询null列表", notes = "查询null列表", httpMethod = "GET")
    @RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreListResponse<SubAccountVo>> queryList(@ModelAttribute @Valid SubAccountQueryForm form) throws BusinessServiceException {
        SubAccountCondition condition = this.getConditionByQueryForm(form);
        List<SubAccountPo> poList = subAccountService.queryList(condition);
        List<SubAccountVo> voList = CopyUtil.transfer(poList, SubAccountVo.class);
        return getSuccessResult(getListResponse(voList));
    }


    @ApiOperation(value = "根据子账号查询主账号", notes = "根据子账号查询主账号", httpMethod = "GET")
    @RequestMapping(value = "/queryMainAccountBySubAccount", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<String> queryMainAccountBySubAccount(@ApiParam(value = "subUserCode", required = true) @RequestParam String subUserCode) throws BusinessServiceException {
        SubAccountCondition condition = new SubAccountCondition();
        condition.setSubUserCode(subUserCode);
        List<SubAccountPo> poList = subAccountService.queryList(condition);
        List<SubAccountVo> voList = CopyUtil.transfer(poList, SubAccountVo.class);
        String subCode = "";
        if (!voList.isEmpty()) {
            subCode = voList.get(0).getUserCode();
        }

        return getSuccessResult("", subCode);
    }


    @ApiOperation(value = "查询null列表(带分页)", notes = "查询null列表(带分页)", httpMethod = "GET")
    @RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreCutPageResponse<SubAccountVo>> queryPageList(@ModelAttribute @Valid SubAccountQueryForm form) throws BusinessServiceException {
        SubAccountCondition condition = this.getConditionByQueryForm(form);
        List<SubAccountVo> voList = new ArrayList<>();
        int count = subAccountService.queryCount(condition);
        if (count > 0) {
            List<SubAccountPo> poList = subAccountService.queryList(condition);
            voList = CopyUtil.transfer(poList, SubAccountVo.class);
        }
        return getSuccessResult(getPageResponse(form, count, voList));
    }

    @ApiOperation(value = "新增null", notes = "新增null", httpMethod = "POST")
    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<SubAccountVo> add(@ModelAttribute @Valid SubAccountCreateForm form) throws BusinessServiceException {
        SubAccountPo po = CopyUtil.transfer(form, SubAccountPo.class);
        subAccountService.insert(po);
        SubAccountVo vo = CopyUtil.transfer(po, SubAccountVo.class);
        return getSuccessResult(vo);
    }

    @ApiOperation(value = "修改null", notes = "修改null", httpMethod = "POST")
    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity update(@ModelAttribute @Valid SubAccountUpdateForm form) throws BusinessServiceException {
        SubAccountPo po = CopyUtil.transfer(form, SubAccountPo.class);
        subAccountService.update(po);
        return getSuccessResult();
    }

    @ApiOperation(value = "删除null", notes = "删除null", httpMethod = "POST")
    @RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity delete(@ApiParam(value = "ID", required = true) @RequestParam Integer id) throws BusinessServiceException {
        subAccountService.delete(id);
        return getSuccessResult();
    }

    /**
     * SubAccountQueryForm转换为SubAccountCondition
     *
     * @param form
     * @return
     */
    private SubAccountCondition getConditionByQueryForm(SubAccountQueryForm form) {
        SubAccountCondition condition = CopyUtil.transfer(form, SubAccountCondition.class);
        return condition;
    }

}